<?php

declare (strict_types=1);

namespace app\base\controller;


use think\admin\Controller;
use app\base\model\Notice as NoticeMod;
use think\admin\helper\QueryHelper;

/**
 * 公告管理
 * @class Package
 * @package app\base\controller
 */
class Notice extends Controller
{

   
    /**
     * 公告管理
     * @auth true
     * @menu true
     * @login true
     */
    public function index()
    {


        NoticeMod::mQuery()->layTable(function () {
            $this->title = '公告&服务';

        }, function (QueryHelper $query) {
            $query->where('is_deleted', 0)->like('tile')->order('sort desc');
        });

    }

    /**
     * 公告列表数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _page_filter(array &$data)
    {

    }


    /**
     * 添加公告
     * @auth true
     */
    public function add()
    {
        $this->title = '添加公告';
        NoticeMod::mForm('form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isPost()) {

            if(isset($data['attachment']) && !empty($data['attachment'])){
                //去掉空值
                $data['attachment'] = array_filter($data['attachment']);
                $data['attachment'] = json_encode(array_values($data['attachment']));
            }

        }else{
            if(isset($data['attachment'])){
                $data['attachment'] = json_decode($data['attachment'],true);
                $data['attachment_num'] = count($data['attachment']);
            }

        }
    }
    /**
     * 编辑公告
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->title = '编辑公告';
        NoticeMod::mForm('form');
    }


    /**
     * 删除公告
     * @auth true
     */
    public function remove()
    {
        NoticeMod::mDelete();
    }

    public function info(){
        $this->title = '公告详情';
        $data = NoticeMod::mk()->find(input('id'));
        if(isset($data['attachment'])){
            $data['attachment'] = json_decode($data['attachment'],true);
            $data['attachment_num'] = count($data['attachment']);
        }
        $this->data = $data;
        $this->fetch();

    }



}